package Test;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

import com.zhen.po.Teacher;


public class TestDaoNamedParamterJDBCTempalte extends NamedParameterJdbcDaoSupport{
//	private DataSource dataSource;
	public void getCourse()
	{
		String sql="SELECT * FROM TEACHER WHERE T_ID=:tid AND T_NAME=:tname";
		Map<String, String> paramMap=new HashMap<String, String>();
		paramMap.put("tid", "1");
		paramMap.put("tname", "LIU");
		List<?>courseList= getNamedParameterJdbcTemplate().queryForList(sql, paramMap);
		for(int i=0;i<courseList.size();i++)
		{
			Map<?, ?>map=(Map<?, ?>) courseList.get(i);
			System.out.println(map.get("T_ID"));
			System.out.println(map.get("T_NAME"));
		}
	}
	public void getBean()
	{
		String sql="SELECT * FROM TEACHER WHERE T_ID=:tid AND T_NAME=:tname";
		Map<String, String> paramMap=new HashMap<String, String>();
		paramMap.put("tid", "1");
		paramMap.put("tname", "LIU");
		List<Teacher>courseList= getNamedParameterJdbcTemplate().query(sql, paramMap, new RowMapper<Teacher>() {

			@Override
			public Teacher mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub		
					Teacher teacher=new Teacher();
					teacher.setT_ID(rs.getInt(1));
					teacher.setT_NAME(rs.getString(2));
					return teacher;
			}
			
		});
		for(Teacher t:courseList)
		{
			System.out.println(t.getT_ID());
			System.out.println(t.getT_NAME());
		}
	}
	public void getBeanTwo()
	{
		String sql="SELECT * FROM TEACHER WHERE T_ID=:tid AND T_NAME=:tname";
		Map<String, String> paramMap=new HashMap<String, String>();
		paramMap.put("tid", "1");
		paramMap.put("tname", "LIU");
		List<Teacher>courseList= getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<Teacher>(Teacher.class));
		for(Teacher t:courseList)
		{
			System.out.println(t.getT_ID());
			System.out.println(t.getT_NAME());
		}
	}

}
